package 多线程练习.JUC;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CyclicBarrierTest {

      public static void main(String[] args) {
            ExecutorService service = Executors.newCachedThreadPool();
            CyclicBarrier cyclicBarrier=new CyclicBarrier(5, new Runnable() {
                  @Override
                  public void run() {
                        System.out.println("全部到达，开始选人物");
                        service.shutdown();
                  }
            });

            for(int i=0;i<5;i++){
                  service.execute(new Runnable() {
                        @Override
                        public void run() {
                              try {
                                    Thread.sleep(4000);
                                    System.out.println(Thread.currentThread().getName()+
                                            "到达栅栏");
                                    cyclicBarrier.await();
                              } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                              } catch (BrokenBarrierException e) {
                                    throw new RuntimeException(e);
                              }
                        }
                  });
            }
      }
}
